﻿using System;
using System.Collections.Generic;
using System.Data;
using SmartMenu.Classes;
using SmartMenu.Providers;
using visinia.SmartEngine.Cache;

namespace SmartMenu.Controllers
{
    public class MenuItemController
    {
        public static List<MenuItemInfo> GetMenuItems()
        {
            string key = "menuItems";
            List<MenuItemInfo> menuItems = (List<MenuItemInfo>)DataCache.GetCache(key);
            if (menuItems == null)
            {
                menuItems = FillMenuItems(SmartMenuDataProvider.Provider.GetMenuItems());
                DataCache.SetCache(key, menuItems, new TimeSpan(0, 30, 0));
            }
            return menuItems;
        }

        public static List<MenuItemInfo> GetMenuItems(int MenuId)
        {
            return GetMenuItems().FindAll(p => p.MenuId == MenuId);
        }

        public static MenuItemInfo GetMenuItem(int MenuItemId)
        {
            return GetMenuItems().Find(p => p.MenuItemId == MenuItemId);
        }

        public static int AddMenuItem(MenuItemInfo menuItem)
        {
            IDataReader dr = SmartMenuDataProvider.Provider.AddMenuItem(menuItem.MenuId, menuItem.ParentId, menuItem.Title, menuItem.Url, menuItem.Order, menuItem.Visible, menuItem.Image);
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            DataCache.DeleteCacheMultiple("menuItems");
            dr.Close();
            return index;
        }

        public static void UpdateMenuItem(MenuItemInfo menuItem)
        {
            SmartMenuDataProvider.Provider.UpdateMenuItem(menuItem.MenuItemId, menuItem.MenuId, menuItem.ParentId, menuItem.Title, menuItem.Url, menuItem.Order, menuItem.Visible, menuItem.Image);
            DataCache.DeleteCacheMultiple("menuItems");
        }

        public static void DeleteMenuItem(int menuItemId)
        {
            SmartMenuDataProvider.Provider.DeleteMenuItem(menuItemId);
            DataCache.DeleteCacheMultiple("menuItems");
        }

        #region PRIVATE METHODS
        private static List<MenuItemInfo> FillMenuItems(IDataReader dr)
        {
            List<MenuItemInfo> list = new List<MenuItemInfo>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr));
            }
            dr.Close();
            return list;
        }

        private static MenuItemInfo ActualRead(IDataReader dr)
        {
            MenuItemInfo menuItem = new MenuItemInfo();
            if (dr["MenuItemId"] != Convert.DBNull)
                menuItem.MenuItemId = (int)dr["MenuItemId"];
            if (dr["MenuId"] != Convert.DBNull)
                menuItem.MenuId = (int)dr["MenuId"];
            if (dr["ParentId"] != Convert.DBNull)
                menuItem.ParentId = (int)dr["ParentId"];
            if (dr["Title"] != Convert.DBNull)
                menuItem.Title = (string)dr["Title"];
            if (dr["Url"] != Convert.DBNull)
                menuItem.Url = (string)dr["Url"];
            if (dr["Order"] != Convert.DBNull)
                menuItem.Order = (int)dr["Order"];
            if (dr["Visible"] != Convert.DBNull)
                menuItem.Visible = (bool)dr["Visible"];
            if (dr["Image"] != Convert.DBNull)
                menuItem.Image = (string)dr["Image"];

            return menuItem;
        }
        #endregion
    }

}
